﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

namespace DeviceTestStand
{
    public partial class FPanel : Panel
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
        private Color _borderColor = Color.Black;
        private int _borderWidth = 1;

        public FPanel()
        {
            InitializeComponent();
        }

        public FPanel(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, false);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

        private Color _BorderColor = Color.Gray;

        [Browsable(true), Description("边框颜色"), Category("自定义分组")]
        public Color BorderColor
        {
            get { return _BorderColor; }
            set
            {
                _BorderColor = value;
                this.Invalidate();
            }
        }

        private int _BorderSize = 1;

        [Browsable(true), Description("边框粗细"), Category("自定义分组")]
        public int BorderSize
        {
            get { return _BorderSize; }
            set
            {
                _BorderSize = value;
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            IntPtr hDC = GetWindowDC(this.Handle);
            Graphics g = Graphics.FromHdc(hDC);
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
                 this._BorderColor, this._BorderSize, ButtonBorderStyle.Solid,//左边
                 this._BorderColor, this._BorderSize, ButtonBorderStyle.Solid,//上边
                 this._BorderColor, this._BorderSize, ButtonBorderStyle.Solid,//右边
                 this._BorderColor, this._BorderSize, ButtonBorderStyle.Solid);//底边
            g.Dispose();
            ReleaseDC(Handle, hDC);
        }
    }
}
